<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right"
                   style="font-size: 18px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>年级列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-table
            :data="gradeArr"
            border
            style="width: 100%">
      <el-table-column prop="id" label="ID" width="70" align="center">
      </el-table-column>
      <el-table-column prop="year" label="年级" width="150" align="center">
      </el-table-column>
      <el-table-column prop="num" label="学生人数" width="100" align="center">
      </el-table-column>
      <el-table-column prop="teacherName" label="负责老师" width="100" align="center">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
          @click="handleEdit(scope.row)"></el-button>
<!--          <el-button type="danger" icon="el-icon-delete" circle size="mini"-->
<!--                     @click="openDeleteConfirm(scope.row)"></el-button>-->
        </template>  <!--scope.row表示当前行-->
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑年级的对话框 -->
    <el-dialog title="修改年级" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="年级" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.year" disabled></el-input>
        </el-form-item>
        <el-form-item label="学生人数" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.num"></el-input>
        </el-form-item>
        <el-form-item label="负责老师" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.teacherId" placeholder="请选择">
            <el-option
                v-for="item in teacherListOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        teacherListOptions: [],
        dialogFormVisible: false,
        gradeArr: [],
        ruleForm: {
          id: '',
          year:'',
          num: '',
          teacherName: '',
          teacherId:''
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      submitEdit() {
        let url = 'http://localhost:9801/grade/' + this.ruleForm.id +'/update';
        console.log('url = ' + url);
        let formData = this.qs.stringify(this.ruleForm);
        console.log('formData = ' + formData);
        this.axios.post(url, formData).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.$message({
              message: '修改年级成功！',
              type: 'success'
            });
            this.dialogFormVisible = false;
            this.loadGradeList();
          } else if (responseBody.state == 40900) {
            this.$message.error(responseBody.message);
          } else {
            this.$message.error(responseBody.message);
            this.dialogFormVisible = false;
            this.loadAlbumList();
          }
        });
      },
      handleEdit(grade){
        let message = '您正在尝试编辑【' + grade.id + '-' + grade.year + '】的专业详情……';
        console.log(message);
        let url = 'http://localhost:9801/grade/' + grade.id;
        console.log(url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.ruleForm = responseBody.data;
            this.dialogFormVisible = true;
          } else {
            this.$message.error(responseBody.message);
            this.loadGradeList();
          }
        });
      },
      handleDelete(grade){
        console.log('准备执行删除, id=' + grade.id);
        let url = 'http://localhost:9801/grade/' + grade.id + '/delete';
        console.log('url=' + url);
        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state != 20000){  //如果删除不成功则提示错误信息
            this.$message.error(responseBody.message);
          }
            this.loadGradeList();  //刷新页面
        })
      },
      openDeleteConfirm(grade){
        let message = '此操作将永久删除【' + grade.majorName+ '】专业, 是否继续?';
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(grade);
        }).catch(() => {
          //点击取消后的操作
        });
      },
      loadGradeList() {
        console.log('loadGradeList');
        let url = 'http://localhost:9801/grade';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.gradeArr = responseBody.data;
        });
      },
      loadTeacherList() {
        console.log('loadTeacherList');
        let url = 'http://localhost:9800/teacher';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.teacherListOptions = responseBody.data;
        });
      }
    },
    mounted() {  //挂载，在created后面执行的
      console.log('mounted');
      this.loadGradeList();
      this.loadTeacherList();
    }
  }
</script>
